﻿using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Input;

namespace FrontDisplay
{
    /// <summary>
    /// Interaction logic for UserPage.xaml
    /// </summary>
    public partial class MenuPage : UserControl
    {
        private List<MenuPageButton> _list;
        
        private static MenuPage _instance;

        public static MenuPage Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new MenuPage();
                }
                return _instance;
            }
        }

        public MenuPage()
        {
            InitializeComponent();
            MainPanel.ControlsPerPage = 3;
        }

        public void Initialize(IEnumerable<IPlugin> plugins, ContextMenu contextMenu)
        {
            if (_list != null) return;

            _list = new List<MenuPageButton>();

            foreach (var plugin in plugins)
            {
                if (plugin.PageControl == null) continue;
                MenuPageButton button = new MenuPageButton(plugin, contextMenu);
                _list.Add(button);
                MainPanel.Add(button);
            }

            Def.Localization.CultureChanged += LocalizationCultureChanged;
        }

        private void LocalizationCultureChanged(System.Globalization.CultureInfo obj)
        {
            _list.ForEach(item => item.Update());
        }

        private void BackButtonMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.NavigateBack();
        }
    }
}
